package mianyang.zhangsan.com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import mianyang.zhangsan.com.mapper.BookOrderMapper;
import mianyang.zhangsan.com.pojo.BookOrder;
import mianyang.zhangsan.com.pojo.OrderCustom;
import mianyang.zhangsan.com.service.BookOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("bookOrderService")
public class BookOrderServiceImpl implements BookOrderService {
    @Autowired
    private BookOrderMapper bookOrderMapper;

    /**
     * 分页查询所有的订单
     * @param pageNo
     * @return
     */
    @Override
    public Page<BookOrder> queryPages(int pageNo) {
        Page<BookOrder> bookOrderPage = bookOrderMapper.selectPage(new Page<>(pageNo, 3), null);
        return bookOrderPage;
    }

    /**
     * 添加订单
     * @param order
     */
    @Override
    public void addOrder(BookOrder order) {
        bookOrderMapper.insert(order);
    }

    /**
     * 封装订单信息
     * @param item
     * @return
     */
    @Override
    public OrderCustom packing(BookOrder item) {
        OrderCustom orderCustom = new OrderCustom();
        orderCustom.setOrderId(item.getOrderId());
        orderCustom.setRoomNo(item.getRoom());
        orderCustom.setUsername(item.getUser());
        orderCustom.setDays(item.getDays());
        orderCustom.setLiveDate(item.getLiveDate());
        orderCustom.setOrderState(item.getOrderState());
        orderCustom.setOrderMemo(item.getOrderMemo());
        orderCustom.setMoney(item.getMoney());
        orderCustom.setOrderTime(item.getOrderTime());
        return orderCustom;
    }

    /**
     * 更新订单信息
     * @param orderId
     * @param order
     */
    @Override
    public void updateOrder(int orderId, BookOrder order) {
        QueryWrapper<BookOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("orderid", orderId);
        bookOrderMapper.update(order, wrapper);
    }

    /**
     * 根据条件分页查询订单信息
     * @param page
     * @param rows
     * @param order
     * @return
     */
    @Override
    public Page<BookOrder> queryPagesWrapper(int page, int rows, BookOrder order) {
        QueryWrapper<BookOrder> wrapper = queryWrapper(order);
        return bookOrderMapper.selectPage(new Page<>(page, rows), wrapper);
    }

    /**
     * 删除一条订单信息
     * @param orderId
     */
    @Override
    public void delete(String orderId) {
        int order = Integer.parseInt(orderId);
        QueryWrapper<BookOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("orderid", order);
        bookOrderMapper.delete(wrapper);
    }

    /**
     * 删除多条订单信息
     * @param orderIds
     */
    @Override
    public void deletes(String orderIds) {
        String[] orders = orderIds.split(",");
        for (String order : orders){
            delete(order);
        }
    }

    /**
     * 根据查询条件查询所有订单信息
     * @param order
     * @return
     */
    @Override
    public List<BookOrder> queryListByWrapper(BookOrder order) {
        QueryWrapper<BookOrder> wrapper = queryWrapper(order);
        return bookOrderMapper.selectList(wrapper);
    }

    /**
     * 封装查询信息
     * @param order
     * @return
     */
    @Override
    public QueryWrapper<BookOrder> queryWrapper(BookOrder order) {
        QueryWrapper<BookOrder> wrapper = new QueryWrapper<>();
        if (order.getRoom() != null && !"".equals(order.getRoom())){
            wrapper.eq("roomobj", order.getRoom());
        }
        if (order.getRoomType() != null && !"".equals(order.getRoomType())){
            wrapper.eq("roomtypeobj", order.getRoomType());
        }
        if (order.getUser() != null && !"".equals(order.getUser())){
            wrapper.eq("userobj", order.getUser());
        }
        if (order.getLiveDate() != null && !"".equals(order.getLiveDate())){
            wrapper.eq("livedate", order.getLiveDate());
        }
        if (order.getOrderState() != null && !"".equals(order.getOrderState())){
            wrapper.eq("orderstate", order.getOrderState());
        }
        if (order.getOrderTime() != null && !"".equals(order.getOrderTime())){
            wrapper.eq("ordertime", order.getOrderTime());
        }
        return wrapper;
    }
}
